package org.mixare.gui;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class Globals{

	public static final String SERVER_ADR = "http://catchmesrv2.appspot.com/catchme";
	protected static final int MIN_PASS_LENGTH = 8;
	public static final double CATCHING_DISTANCE = 10.0;
	public static final int RESPONSE_OK = 200;
	public static final String MESSAGE_OK = "OK";
	public static final String USERNAME = "username";
	public static final String LEVEL = "level";
	public static final String EXP = "exp";

	public static String sendPostRequest(String params) throws Exception{ 
		URL url;
		HttpURLConnection conn = null;

		url = new URL(Globals.SERVER_ADR);
		conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
		wr.write(params);
		wr.close();

		int code = conn.getResponseCode();
		BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		String line = rd.readLine();
		if(rd.readLine() != null){
			throw new IllegalArgumentException();
		}
		rd.close();
		conn.disconnect();
		return code + ";" + line;
	}
	
	public static String getJsonParameter(String json, String param){
		int start = json.indexOf(param) + param.length() + 2;
		int end = json.indexOf(",", start);
		if(end == -1){
			end = json.indexOf("}");
		}
		return json.substring(start, end);
	}
}